<?php
namespace app\shop\controller;
use think\Request;
use think\Exception;
use app\shop\model\ClientLevel as ClientLevelModel;

class Clientlevel extends \ShopController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $clientLevelModel = new ClientLevelModel();

        try {
            $data = $clientLevelModel->where([
                'is_delete' => 0,
            ])->order('level_id')->select();
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $customerLevelModel = new ClientLevelModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $clientLevelModel = new ClientLevelModel();

        $data = $request->post();

        /* @var $validate \ShopValidate */
        $validate = \think\Loader::validate('ClientLevel');
        if ( !$validate->scene('')->check($data) ) {
            return $validate->getError();
        }

        try {

            $result = $clientLevelModel->data($data)->allowField(true)->save();

            if (! $result) {
                $this->serverErrorTerminate('资源新增失败');
            }

            $this->successTerminate($clientLevelModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $clientLevelModel = new ClientLevelModel();

        try {
            $data = $clientLevelModel->get($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $clientLevelModel = new ClientLevelModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $clientLevelModel = new ClientLevelModel();
        $data = $request->post();

        $pk = $clientLevelModel->getPk();
        $id = $data[$pk];
        if ( isset($data[$pk]) ) unset($data[$pk]);

        try {
            $affectedRowCount = $clientLevelModel->allowField(true)->save($data, [
                $pk => $id,
            ]);
            if (! $affectedRowCount) $this->serverErrorTerminate('更新资源失败');
            $this->successTerminate($clientLevelModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

//    /**
//     * 删除指定资源
//     *
//     * @param  int  $id
//     * @return \think\Response
//     */
//    public function delete($id)
//    {
//        $clientLevelModel = new ClientLevelModel();
//
//        try {
//
//            $this->successTerminate();
//        } catch (Exception $e) {
//            $this->serverErrorTerminate($e->getMessage());
//        }
//    }
}
